//
//  KKStatusRetweetView.swift
//  KKWeibo
//
//  Created by kassia on 16/7/14.
//  Copyright © 2016年 kassia. All rights reserved.
//

import UIKit
import SnapKit
/// 转发微博视图
class KKStatusRetweetView: UIView {


    // 定义转发视图的地步约束
    var retweetViewBottom: Constraint?
    // 赋值
    var statusViewModel: KKStatusViewModel? {
        
        didSet {
            
            contentLable.text = statusViewModel?.retweetContent
            
            //卸载约束
            retweetViewBottom?.uninstall()
            
            if let count = statusViewModel?.status?.retweeted_status?.pic_urls?.count where count > 0 {
            
            pictureView.picUrls = statusViewModel?.status?.retweeted_status?.pic_urls 
                //有配图
            self.snp_updateConstraints(closure: { (make) -> Void in
                    
            self.retweetViewBottom = make.bottom.equalTo(pictureView).offset(originalMagrinCell).constraint
                
                })
                
              pictureView.hidden = false
                
            } else {
            
            pictureView.hidden = true

            //没有配图
            self.snp_updateConstraints(closure: { (make) -> Void in
                    
            self.retweetViewBottom = make.bottom.equalTo(contentLable).offset(originalMagrinCell).constraint
                    
                })

            }
            
        }
        
    }

    // 懒加载控件
    private lazy var contentLable: UILabel = {
    
        let lable = UILabel ()
        lable.numberOfLines = 0
        lable.font = UIFont.systemFontOfSize(14)
        
        return lable
    
    }()
    private lazy var pictureView: KKStatusPictureView = {
    
        let pictureView = KKStatusPictureView ()
        
        pictureView.backgroundColor = self.backgroundColor
        
        return pictureView
        
    }()
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setUpUI() {
    
       //contentLable.backgroundColor = UIColor(white: 0.97, alpha: 1)
        
        addSubview(contentLable)
        addSubview(pictureView)
        
        contentLable.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self).offset(originalMagrinCell)
            make.leading.equalTo(self).offset(originalMagrinCell)
            make.trailing.equalTo(self).offset(-originalMagrinCell)
            
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentLable.snp_bottom).offset(originalMagrinCell)
            make.leading.equalTo(contentLable).offset(-originalMagrinCell)


        }

        //  关键的约束
        //  转发微博视图的底部约束等于我们转发内容label的底部约束加上一个间距
    
        self.snp_makeConstraints { (make) -> Void in
          self.retweetViewBottom = make.bottom.equalTo(pictureView).offset(originalMagrinCell).constraint
        }
    }
}
